import { Global_Date } from "../Global";

const { ccclass, property } = cc._decorator;

@ccclass
export default class GameLogic extends cc.Component {
	@property(cc.Label)
	scoreLabel: cc.Label = null;
	
	@property({ type: cc.SpriteAtlas, tooltip: "西瓜图片" })
	spriteAtlas: cc.SpriteAtlas = null
	
    @property(cc.Node)
	typeNode: cc.Node = null

	onLoad() {
		this.node.on("addScore", this.addScore, this);
		this.node.on('random-type',this.randomType,this);
	}

	start() {}

	// update (dt) {}

	addScore() {
		if (this.scoreLabel) {
			this.scoreLabel.string = Global_Date.SCORE.toString();
		}
		cc.sys.localStorage.setItem("score", Global_Date.SCORE);
	}
	
	
	//转换图片
	randomType() {
		let randomNum = Math.floor(Math.random() * 5) | 0
		let sprite = this.typeNode.getComponent(cc.Sprite)
		sprite.spriteFrame = this.spriteAtlas.getSpriteFrames()[randomNum]
		Global_Date.WATERMELON_TYPE = randomNum
	}
}
